/**
 * Created by Baggio on 2019-1-14.
 */


$(function () {

    var postData = {};
    $.ajax({
        type: "get",
        url: "/order/getOrderList",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            console.log(res.data);

            showData(res.data);
        }

    });
});


function showData(data) {

    var html = "";

    for (var i = 0; i < data.length; i++) {
        html += " <div class=\"box_1\">";
        html += " <div class=\"top\">";
        html += "     <span class=\"time\">" + new Date(data[i].createTime).Format("yyyy-MM-dd hh:mm") + "</span>";
        html += "     <span class=\"number\">订单号:" + data[i].orderId + "</span>";
        html += "  <span class=\"state\">" + showStatus(data[i].status) + "</span>";
        html += "     </div>";
        for (var j = 0; j < data[i].orderItemList.length; j++) {
            html += "      <div class=\"information_box\">";
            html += "    <div class=\"img_box\"><img src=\"" + data[i].orderItemList[j].productEntity.picUrl + "\" alt=\"\"></div>";
            html += "     <div class=\"information_box_1\">";
            html += "    <h5>" + data[i].orderItemList[j].productEntity.name + "</h5>";
            html += "   <h5><span class=\"color\">数量：</span><span>" + data[i].orderItemList[j].amount + "</span></h5>";
            /*  html += "  <h5><span class=\"package\">套餐：</span><span>套餐一</span></h5>";*/
            html += "   <h5>￥" + data[i].orderPrice / 100.00 + "</h5>";
            html += "   </div>";
            html += "  </div>";
        }


        html += "   <div class=\"state_box\">";
        if (data[i].status == 13) {
            html += "    <a href=\"javascript:void(0);\" class=\"btn btn_1\" onclick='confirmOrder(" + data[i].orderId + ")'>确认收货</a>";
        }


        html += "     </div>";
        html += "    </div>";
    }
    $("#orderList").html(html);
}

function confirmOrder(orderId) {
    alert(orderId);
    var postData = {orderId: orderId};
    $.ajax({
        type: "post",
        url: "/order/completeOrder",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            console.log(res.data);
            location.reload();
        }

    });
}
function showStatus(status) {
    var s = "";
    switch (status) {
        case 1:
            s = "未支付";
            break;
        case 2:
            s = "已支付";
            break;
        case 13:
            s = "已发货";
            break;
        case 9:
            s = "交易成功";
            break;
        default:
            s = "";
            break;
    }

    return s;
}